package com.example.blog.web.userController;

import com.example.blog.pojo.Comment;
import com.example.blog.service.BlogService;
import com.example.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private BlogService blogService;
    @GetMapping("/comments/{id}")
    public String comments(@PathVariable Long id, Model model){

        model.addAttribute("comments",commentService.listCommentByBlogId(id));
        model.addAttribute("blog",blogService.getBLog(id));
        return "blog::commentList";
    }
    @PostMapping("/comments")
    public String post(Comment comment){

        Long id = comment.getBlog().getId();
        comment.setBlog(blogService.getBLog(id));
        commentService.saveComment(comment);
        return "redirect:/comments/"+id;
    }
}
